package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.di.component.DaggerSecondHandImportToStoreComponent;
import com.kmproject.haifu.mvp.contract.SecondHandImportToStoreContract;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandInfoBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ObtainParmartBean;
import com.kmproject.haifu.mvp.presenter.SecondHandImportToStorePresenter;
import com.kmproject.haifu.mvp.ui.adapter.SecondHandImportToStoreAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 09/04/2019 10:59
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class SecondHandImportToStoreActivity extends BaseActivity<SecondHandImportToStorePresenter> implements SecondHandImportToStoreContract.View {

    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.store_secondHand_RecyclerView)
    RecyclerView storeSecondHandRecyclerView;
    @BindView(R.id.secondHand_refreshLayout)
    SmartRefreshLayout secondHandRefreshLayout;
    @BindView(R.id.collect_house_rent_checkBox)
    CheckBox collectHouseRentCheckBox;
    @BindView(R.id.collect_house_rent_allSelect_lay)
    LinearLayout collectHouseRentAllSelectLay;
    @BindView(R.id.activity_house_rent_show_del)
    TextView activityHouseRentShowDel;
    @BindView(R.id.collect_house_rent_bottom_lay)
    LinearLayout collectHouseRentBottomLay;
    ObtainParmartBean obtainParmartBean=new ObtainParmartBean();
    private SecondHandImportToStoreAdapter secondHandImportToStoreAdapter;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSecondHandImportToStoreComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_second_hand_import_to_store; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        toolbarTitle.setTextColor(ArmsUtils.getColor(this, R.color.white));
        toolbar.setBackgroundColor(ArmsUtils.getColor(this, R.color.color_3F3F4B));
        toolbarBackImg.setImageResource(R.drawable.member_center_main_back_img);
        secondHandRefreshLayout.setEnableRefresh(false);
        storeSecondHandRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        secondHandImportToStoreAdapter=new SecondHandImportToStoreAdapter(new ArrayList<>());
        storeSecondHandRecyclerView.setAdapter(secondHandImportToStoreAdapter);
        secondHandImportToStoreAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        secondHandImportToStoreAdapter.setShowCheck(true);
        secondHandImportToStoreAdapter.setIsselectAll(0);
       mPresenter.querySecondHandToImport(obtainParmartBean);
        secondHandRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                obtainParmartBean.setPage(obtainParmartBean.getPage()+1);
                mPresenter.querySecondHandToImport(obtainParmartBean);
            }
        });
        activityHouseRentShowDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
       List<String> temIds=mPresenter.getSelectIndex(secondHandImportToStoreAdapter.getData());
               if (temIds.isEmpty())
               {
                   showMessage("请选择要导入的物品");
                   return;
               }
              mPresenter.importSecondhanToStore(getIntent().getStringExtra("userStoreId"),temIds);
            }
        });
        collectHouseRentCheckBox.setChecked(false);
        collectHouseRentCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
                    secondHandImportToStoreAdapter.setIsselectAll(1);
                }else {
                    secondHandImportToStoreAdapter.setIsselectAll(2);
                }
            }
        });
    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void importSecondhanToStoreSuccess() {
      showMessage("导入成功");
      secondHandImportToStoreAdapter.setNewData(null);
        obtainParmartBean=new ObtainParmartBean();
        mPresenter.querySecondHandToImport(obtainParmartBean);

    }

    @Override
    public void querySecondHandToImportSuccess(int curryPage, int totalPage, List<SecondHandInfoBean> secondHandInfoBeans) {
        secondHandRefreshLayout.finishLoadMore();
        if (secondHandInfoBeans == null || secondHandInfoBeans.isEmpty()) {
            secondHandRefreshLayout.setEnableLoadMore(false);
            if (curryPage == 1) {
                secondHandImportToStoreAdapter.setEmptyView(AppUtil.setEmptyView(SecondHandImportToStoreActivity.this, "未搜索到二手物品信息", "", null, true));
            }
            return;
        }
        obtainParmartBean.setPage(curryPage);
        if (curryPage >= totalPage) {
            secondHandRefreshLayout.setEnableLoadMore(false);
        } else {
            secondHandRefreshLayout.setEnableLoadMore(true);
        }
        if (secondHandImportToStoreAdapter.getData() == null || secondHandImportToStoreAdapter.getData().isEmpty()) {
            secondHandImportToStoreAdapter.setNewData(secondHandInfoBeans);
        } else {
            secondHandImportToStoreAdapter.addData(secondHandInfoBeans);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
